package com.jpl.seminar.ui.module.registration;

import java.util.List;
import java.util.Locale;

import com.jpl.seminar.model.Participant;
import com.jpl.seminar.model.Registration;
import com.jpl.seminar.ui.util.DemoHelper;
import com.jpl.seminar.ui.util.ServiceLocator;
import com.vaadin.data.Item;
import com.vaadin.data.Validator.EmptyValueException;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormFieldFactory;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

public class MobileRegistrationForm extends Form {

	private static final long serialVersionUID = 1L;

	public MobileRegistrationForm() {
		super();
	}

	public void init() {

		// bind form to a BeanItem
		final Registration registration = new Registration();
		BeanItem<Registration> registrationBean = new BeanItem<Registration>(registration);
		setItemDataSource(registrationBean);

		// set how the field are gonna be rendered using a FormFieldFactory
		setFormFieldFactory(new RegistrationFormFieldFactory());

		// Set the order of the items in the form.
		setVisibleItemProperties(DemoHelper.getFormFieldsOrder());

		// add footer layout to contains submit and cancel button
		HorizontalLayout footerLayout = new HorizontalLayout();
		footerLayout.setWidth("100%");
//		footerLayout.setHeight("25px");
		setFooter(footerLayout);
		Button sendButton = new Button("Envoyer");
		footerLayout.addComponent(sendButton);
		footerLayout.setComponentAlignment(sendButton, Alignment.MIDDLE_RIGHT);

		sendButton.addListener(new Button.ClickListener() {

			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				getWindow().showNotification("envoi du formulaire");
				try {
					commit();
					ServiceLocator.getInstance().getSeminarService().save(registration);
				} catch (EmptyValueException e) {
					System.out.println("validation error");
				}
			}
		});

		// make sure validation is immediate
		setImmediate(true);
	}

	class RegistrationFormFieldFactory implements FormFieldFactory {

		private static final long serialVersionUID = 1L;

		public Field createField(Item item, Object propertyId, Component uiContext) {
			// Identify the fields by their Property ID.
			Field result = null;
			String pid = (String) propertyId;
			if (ModelPropertiesConstant.REGISTRATION_COMPANY_FIELD.equals(pid)) {
				TextField textField = DemoHelper.createTextField("Company", "90%", true, "company's name missing");
				textField.focus(); // set focus on first field
				result = textField;
			} else if (ModelPropertiesConstant.REGISTRATION_ADDRESS_FIELD.equals(pid)) {
				TextArea textArea = DemoHelper.createTextArea("Address", "90%", 3, true, "company's address missing");
				result = textArea;
			} else if (ModelPropertiesConstant.REGISTRATION_NPA_AND_CITY_FIELD.equals(pid)) {
				TextField textField = DemoHelper.createTextField("NPA/City:", "90%", true, "company's npa/city missing");
				result = textField;
			} else if (ModelPropertiesConstant.REGISTRATION_PHONE_FIELD.equals(pid)) {
				TextField textField = DemoHelper.createTextField("Phone", "90%", true, "company's phone missing");
				result = textField;
			} else if (ModelPropertiesConstant.REGISTRATION_FAX_FIELD.equals(pid)) {
				TextField textField = DemoHelper.createTextField("Fax:", "90%", false, null);
				result = textField;
			} else if (ModelPropertiesConstant.REGISTRATION_COUNTRY_FIELD.equals(pid)) {
				ComboBox comboBox = DemoHelper.getCountryComboBox("Country:", "90%", true, "company's country missing", Locale.ENGLISH);
				result = comboBox;
			} else if (ModelPropertiesConstant.REGISTRATION_PARTICIPANTS_FIELD.equals(pid)) {
				List<Participant> participants = (List<Participant>) item.getItemProperty(ModelPropertiesConstant.REGISTRATION_PARTICIPANTS_FIELD).getValue();
				ParticipantCustomField participantCustomField = new ParticipantCustomField();
				participantCustomField.setWidth("90%");
				participantCustomField.setCaption("Participants:");
				result = participantCustomField;
			}

			return result; // Invalid field (property) name.
		}
	}

}
